package com.tianci.appstore.ui.detail;

import android.content.Context;
import android.graphics.Bitmap;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.LinearLayout;

import com.coocaa.x.uipackage.widget.WaitingBar;
import com.tianci.appstore.R;
import com.tianci.appstore.controller.AppStoreDetailPageController;
import com.tianci.appstore.ui.SkinManager;
import com.tianci.appstore.ui.data.SkyAppDetailData;
import com.tianci.appstore.ui.data.SkyAppDetailData.AppAction;
import com.tianci.appstore.ui.data.SkyAppDetailData.AppStatus;
import com.tianci.appstore.ui.util.UICommon;
import com.tianci.appstore.ui.widget.ColorButton;
import com.tianci.appstore.ui.widget.ProgressBar;
import com.tianci.appstore.utils.SkyConstants;

public class StatusIndicator extends LinearLayout implements OnClickListener {
    private Context context;
    private SkyAppDetailData data;
    private AppStatus status = AppStatus.NOT_DOWNLOADED;
    private View fakeFocusView;
    private LinearLayout mainLayout;
    private ProgressBar progress;
    private WaitingBar waitingBar;
    private Bitmap waitingBitmap;

    private LayoutParams verticalBtnParams;
    private boolean shouldFocus;

    private int btnPadding = SkyConstants.Div(12);

    public StatusIndicator(Context context) {
        super(context);
        this.context = context;
        fakeFocusView = new View(context);
        this.addView(fakeFocusView, new LayoutParams(1, 1));
        mainLayout = new LinearLayout(context);
        mainLayout.setGravity(Gravity.RIGHT | Gravity.BOTTOM);
        mainLayout.setOrientation(LinearLayout.VERTICAL);
        this.addView(mainLayout, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));

        this.setPadding(0, 0, SkyConstants.Div(33), SkyConstants.Div(30));
        // TODO Auto-generated constructor stub
        verticalBtnParams = new LayoutParams(UICommon.btnWidth, UICommon.btnHeight);
        verticalBtnParams.setMargins(0, SkyConstants.Div(8), 0, 0);
    }

    public void initFocus() {
        fakeFocusView.setFocusable(true);
        fakeFocusView.requestFocus();
    }

    public void setData(SkyAppDetailData data) {
        this.data = data;
    }

    public void updateStatus(AppStatus status) {
        refreshView(this.status, status, false);
    }

    public void updateStatus(AppStatus status, boolean grabFocus) {
        refreshView(this.status, status, grabFocus);
    }

    public void updateDownloadPercent(int percent) {
        if (status == AppStatus.DOWNLOADING
                || status == AppStatus.DOWNLOAD_PAUSED) {
            if (progress != null) {
                progress.setProgress(percent);
            }
        }
    }

    private void refreshView(AppStatus oldStatus, AppStatus newStatus, boolean grabFocus) {
        if (oldStatus == newStatus && newStatus != AppStatus.NOT_DOWNLOADED)
            return;
        if (oldStatus == AppStatus.INSTALLING || oldStatus == AppStatus.WAITING) {
            if (waitingBar != null) {
                waitingBar.stopAnim();
            }
        }

        shouldFocus = this.getFocusedChild() == null ? false : true;
        if (grabFocus)
            shouldFocus = true;
        switch (newStatus) {
            case NOT_DOWNLOADED:
                beforeRecreateView();
                mainLayout.addView(createButton(AppAction.ACTION_DOWNLOAD, 0));
                if (grabFocus)
                    this.findViewWithTag(0).requestFocus();
                break;
            case DOWNLOAD_PAUSED:
                if (oldStatus == AppStatus.DOWNLOADING) {
                    ColorButton btn = (ColorButton) this.findViewWithTag(0);
                    btn.setAction(AppAction.ACTION_CONTINUE);
                } else {
                    init2BtnProgress(newStatus);
                }
                break;
            case DOWNLOADING:
                if (oldStatus == AppStatus.DOWNLOAD_PAUSED) {
                    ColorButton btn = (ColorButton) this.findViewWithTag(0);
                    btn.setAction(AppAction.ACTION_PAUSE);
                } else {
                    init2BtnProgress(newStatus);
                }
                break;
            case DOWNLOADED:
                beforeRecreateView();
                mainLayout.addView(createButton(AppAction.ACTION_INSTALL, 0));
                mainLayout.addView(createButton(AppAction.ACTION_DELETE, 1), verticalBtnParams);
                break;
            case WAITING:
            case INSTALLING:
            case UNINSTALLING:
                initWaitingUI(newStatus);
                break;
            case INSTALLED:
                beforeRecreateView();
                mainLayout.addView(createButton(AppAction.ACTION_LAUNCH, 0));
                mainLayout.addView(createButton(AppAction.ACTION_UNINSTALL, 1), verticalBtnParams);
                break;
            case INSTALLED_WITH_CAN_NOT_BE_UNINSTALLED:
                beforeRecreateView();
                mainLayout.addView(createButton(AppAction.ACTION_LAUNCH, 0));
                break;
            case HAS_NEW_VERSION:
                beforeRecreateView();
                mainLayout.addView(createButton(AppAction.ACTION_UPGRADE, 0));
                mainLayout.addView(createButton(AppAction.ACTION_LAUNCH, 1), verticalBtnParams);
                mainLayout.addView(createButton(AppAction.ACTION_UNINSTALL, 2), verticalBtnParams);
                break;
            case HAS_NEW_VERSION_WITH_CAN_NOT_BE_UNINSTALLED:
                beforeRecreateView();
                mainLayout.addView(createButton(AppAction.ACTION_UPGRADE, 0));
                mainLayout.addView(createButton(AppAction.ACTION_LAUNCH, 1), verticalBtnParams);
                break;
        }
        this.status = newStatus;
        resetFocus();
    }

    private void init2BtnProgress(AppStatus status) {
        beforeRecreateView();

        if (progress == null) {
            progress = new ProgressBar(context);
            progress.showProgressNum(SkyConstants.Dpi(36), R.color.progressColor);
            SkinManager.setBackground(progress, R.drawable.progressbar_bg);
            progress.setProgressDrawable(R.drawable.progressbar);
        }
        LayoutParams progressParams = new LayoutParams(SkyConstants.Div(384), SkyConstants.Div(40));
        progressParams.setMargins(SkyConstants.Div(10), SkyConstants.Div(27), SkyConstants.Div(10), SkyConstants.Div(20));
        progress.setProgress(0);
        mainLayout.addView(progress, progressParams);

        LinearLayout btnLayout = new LinearLayout(context);
        btnLayout.setGravity(Gravity.RIGHT);
        btnLayout.addView(createButton(AppAction.ACTION_CANCEL, 1));

        LayoutParams sndBtnParams = new LayoutParams(UICommon.btnWidth, UICommon.btnHeight);
        sndBtnParams.leftMargin = btnPadding;
        AppAction action = AppAction.ACTION_PAUSE;
        if (status == AppStatus.DOWNLOAD_PAUSED)
            action = AppAction.ACTION_CONTINUE;
        btnLayout.addView(createButton(action, 0, false), sndBtnParams);
        mainLayout.addView(btnLayout);
    }

    private void initWaitingUI(AppStatus status) {
        beforeRecreateView();

        if (waitingBar == null) {
            waitingBar = new WaitingBar(context);
            SkinManager.setBackground(waitingBar, R.drawable.progressbar_bg);
            if (waitingBitmap == null)
                waitingBitmap = SkinManager.getResourceBitmap(R.drawable.waiting_front);
            waitingBar.setWaitingBitmap(waitingBitmap,
                    SkyConstants.Div(60), SkyConstants.Div(1), SkyConstants.Div(1), SkyConstants.Div(3), SkyConstants.Div(3));
        }
        LayoutParams progressParams = new LayoutParams(SkyConstants.Div(384), SkyConstants.Div(40));
        progressParams.setMargins(SkyConstants.Div(10), SkyConstants.Div(27), SkyConstants.Div(10), SkyConstants.Div(20));
        mainLayout.addView(waitingBar, progressParams);
        waitingBar.startAnim();

        if (status.equals(AppStatus.INSTALLING)
                || status.equals(AppStatus.UNINSTALLING)) {
            if (status.equals(AppStatus.INSTALLING)) {
                ColorButton btn = createButton(R.string.installing, 0);
                btn.setBtnEnabled(false);
                mainLayout.addView(btn);
            } else {
                ColorButton btn = createButton(R.string.uninstalling, 0);
                btn.setBtnEnabled(false);
                mainLayout.addView(btn);
            }
        } else {
            LinearLayout btnLayout = new LinearLayout(context);
            btnLayout.setGravity(Gravity.RIGHT);
            btnLayout.addView(createButton(AppAction.ACTION_CANCEL, 1, false));

            LayoutParams sndBtnParams = new LayoutParams(UICommon.btnWidth, UICommon.btnHeight);
            sndBtnParams.leftMargin = btnPadding;
            ColorButton btn = createButton(R.string.waiting, 0, false);
            btn.setBtnEnabled(false);
            btnLayout.addView(btn, sndBtnParams);
            mainLayout.addView(btnLayout);
        }
    }


    private void beforeRecreateView() {
        if (shouldFocus) {
            fakeFocusView.setFocusable(true);
            fakeFocusView.requestFocus();
        } else {
            fakeFocusView.setFocusable(false);
        }
        mainLayout.removeAllViews();
    }

    private void resetFocus() {
        if (shouldFocus) {
            if (mainLayout.findViewWithTag(0) != null)
                mainLayout.findViewWithTag(0).requestFocus();
        }
        fakeFocusView.setFocusable(false);
        shouldFocus = false;
    }

    private ColorButton createButton(int strId, int tag) {
        return createButton(strId, tag, true);
    }

    private ColorButton createButton(int strId, int tag, boolean leftNoView) {
        ColorButton btn = new ColorButton(context, strId);
        btn.setTag(tag);
        btn.setId(tag);
        if (leftNoView)
            btn.setNextFocusLeftId(tag);
        return btn;
    }

    private ColorButton createButton(AppAction action, int tag) {
        return createButton(action, tag, true);
    }

    private ColorButton createButton(AppAction action, int tag, boolean leftNoView) {
        ColorButton btn = new ColorButton(context, action);
        btn.setOnClickListener(this);
        btn.setTag(tag);
        btn.setId(tag);
        if (leftNoView)
            btn.setNextFocusLeftId(tag);
        return btn;
    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        if (v.getTag() != null) {
            ColorButton btn = (ColorButton) v;
            AppStoreDetailPageController.getInstance().handlerDetailAction(data.getId(), data.getTitle(), data.getPkgName(), btn.getAction());
        }
    }

    public void recycleBitmaps() {
        if (waitingBitmap != null)
            waitingBitmap.recycle();
    }

    // 从下面的预览图view按“上”键时，焦点需要手动控制
    public void setBtnFocus(boolean left) {
        switch (status) {
            case NOT_DOWNLOADED:
                this.findViewWithTag(0).requestFocus();
                break;
            case DOWNLOAD_PAUSED:
            case DOWNLOADING:
                if (left)
                    this.findViewWithTag(1).requestFocus();
                else
                    this.findViewWithTag(0).requestFocus();
                break;
            case DOWNLOADED:
                this.findViewWithTag(1).requestFocus();
                break;
            case WAITING:
                if (left)
                    this.findViewWithTag(1).requestFocus();
                else
                    this.findViewWithTag(0).requestFocus();
                break;
            case INSTALLING:
            case UNINSTALLING:
                this.findViewWithTag(0).requestFocus();
                break;
            case INSTALLED:
                this.findViewWithTag(1).requestFocus();
                break;
            case HAS_NEW_VERSION:
                this.findViewWithTag(0).requestFocus();
                break;
        }
    }
}
